home *** CD-ROM | disk | FTP | other *** search
-
- /*+
- * File: expand.c
- * Description:
- * Expand mimics Unix's expand. It is not a full implementation.
- * Does not support [-tab1,tab2,..,tabn].
- *
- * Expand is a filter.
- * It processes the named files or the standard input.
- * It writes to standard output with tabs changed into blanks.
- *
- * Usages:
- * expand [-tabstop] [i_files]
- *
- *
- * Author: Mohsen Banan.
- *
- * This program is public domain software, no warranty intended or
- * implied.
- * General permission to copy or modify, but not for profit, is
- * hereby granted.
- *
- * Functions:
- *
- *
- * Audit Trail: $Log: expand.c,v $
- * Revision 1.1 85/08/28 08:38:31 mohsen
- * original posting to the net
- *
- * Revision 1.1 85/08/14 13:44:13 mohsen
- * Initial revision
- *
- *
- *
- -*/
-
- #ifdef RCS_VER
- static char *rcs = "$Header: expand.c,v 1.1 85/08/28 08:38:31 mohsen Exp $";
- #endif
-
- /* #includes */
- #include "mbstd.h"
- #include <stdio.h>
- #include <ctype.h>
- #include <fcntl.h>
- #ifdef unix
- #include "msc3.h"
- #endif
-
- /* #defines */
-
- /* external variables */
-
- /* referenced external function declarations */
-
- /* internal function declarations */
- PUBLIC VOID expand();
- PUBLIC VOID tab_putc();
- PUBLIC VOID cant_open();
- STATIC VOID usage();
-
- /* global variables */
-
- /* static variables */
- STATIC CHAR * prog_name;
- STATIC INT tabstop;
- STATIC INT cpos;
-
- INT
- main (argc, argv)
- INT argc;
- CHAR * argv[];
- {
- expand(argc, argv);
- }
-
-
- /*<
- * Function:expand
- * Description:
- * Parses the command line and calls tab_putc.
- *
- * Returns:
- * VOID
- *
- >*/
- PUBLIC VOID
- expand (argc,argv)
- INT argc;
- CHAR * argv[];
- {
- FILE * i_fp;
- FILE * o_fp;
-
- INT i;
- INT c;
-
- i_fp = stdin;
- o_fp = stdout;
- tabstop = 8;
- prog_name = argv[0];
- for (i=1; i<argc; ++i) {
- if (*argv[i] == '-') {
- /* To handle concatenated switches */
- INT j;
- j=i;
- while (*(++argv[j])) {
- if (isdigit(*(argv[j]))) {
- tabstop = atoi(argv[j]);
- break;
- }
- #if 0
- switch (*argv[j]) {
- case 'o':
- case 'O':
- if ( !(o_fp = fopen(argv[++i], "w")) ) {
- cant_open (prog_name, argv[i]);
- exit (1);
- }
- break;
- default:
- usage();
- exit(1);
- } /* switch (*argv[j]) */
- #endif
- } /* while (*(++argv[j])) */
- } /* if '-' */
- else {
- if (!(i_fp = fopen (argv[i], "r"))) {
- cant_open (prog_name, argv[i]);
- exit (1);
- }
- setmode (fileno(i_fp), O_TEXT);
- setmode (fileno(o_fp), O_TEXT);
- cpos = 0;
- while (( c = getc (i_fp)) != EOF) {
- tab_putc (c, o_fp);
- }
- fclose (i_fp);
- }
- } /* for i<argc */
- fclose (o_fp);
- exit (0);
- }
-
-
- /*<
- * Function:tab_putc
- * Description:
- * Converts tabs to the correct number of spaces (tabstop).
- * tab_putc is recursive.
- *
- * Returns:
- * VOID
- *
- >*/
- PUBLIC VOID
- tab_putc (c, o_fp)
- INT c;
- FILE * o_fp;
- {
- if (c == '\n') {
- cpos = 0;
- putc (c, o_fp);
- } else if ( c== '\t') {
- do {
- tab_putc(' ', o_fp);
- } while (cpos % tabstop);
- }
- else {
- cpos++;
- putc (c, o_fp);
- }
- }
-
- PUBLIC VOID
- cant_open (prog_name,filename)
- CHAR * prog_name;
- CHAR * filename;
- {
- fprintf (stderr, "%s :can not open %s \n", prog_name, filename);
- }
-
- #if 0
- STATIC VOID
- usage ()
- {
- fprintf (stderr, "Usage: %s [-tabstop] [i_files]\n", prog_name);
- }
- #endif
-
-